package com.computercourse.manage_course.controller;

import com.computercourse.api.course.CategoryControllerApi;
import com.computercourse.api.course.CourseControllerApi;
import com.computercourse.api.course.SysDicthinaryControllerApi;
import com.computercourse.framework.domain.course.*;
import com.computercourse.framework.domain.course.ext.CategoryNode;
import com.computercourse.framework.domain.course.ext.CourseInfo;
import com.computercourse.framework.domain.course.ext.CourseView;
import com.computercourse.framework.domain.course.ext.TeachplanNode;
import com.computercourse.framework.domain.course.request.CourseListRequest;
import com.computercourse.framework.domain.course.response.AddCourseResult;
import com.computercourse.framework.domain.course.response.CoursePublishResult;
import com.computercourse.framework.domain.system.SysDictionary;
import com.computercourse.framework.model.response.QueryResponseResult;
import com.computercourse.framework.model.response.ResponseResult;
import com.computercourse.framework.utils.CicOauth2Util;
import com.computercourse.framework.web.BaseController;
import com.computercourse.manage_course.dao.CategoryRepository;
import com.computercourse.manage_course.dao.SysDictionaryRepository;
import com.computercourse.manage_course.dto.MediaUrlResult;
import com.computercourse.manage_course.dto.NameResult;
import com.computercourse.manage_course.service.CourseService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Feng.Yuan
 * @description
 * @date 2020/1/29 5:50 下午
 * @return
 */
@RestController
@RequestMapping("/course")
public class CourseController extends BaseController implements CourseControllerApi, CategoryControllerApi, SysDicthinaryControllerApi {

    @Autowired
    private CourseService courseService;


    @Autowired
    private CategoryRepository categoryRepository;

    @Autowired
    private SysDictionaryRepository dictionaryRepository;


    @Override
    @GetMapping("/teachplan/list/{courseId}")
    public TeachplanNode findTeachplanList(@PathVariable("courseId") String courseId) {
        return courseService.findTeachplanList(courseId);
    }

    @Override
    @PostMapping("/teachplan/add")
    public ResponseResult addTeachplan(@RequestBody Teachplan teachplan) {
        return courseService.addTeachplan(teachplan);
    }

    @Override
    @PostMapping("/coursebase/add")
    public AddCourseResult addCourseBase(@RequestBody CourseBase courseBase) {
        return courseService.addCourseBase(courseBase);
    }

    @Override
    @PostMapping("/coursepic/add")
    public ResponseResult addCoursePic(@RequestParam("courseId") String courseId, @RequestParam("pic") String pic) {
        return courseService.addCoursePic(courseId, pic);
    }

    @Override
    @GetMapping("/coursepic/list/{courseId}")
    public CoursePic findCoursePic(@PathVariable("courseId") String courseId) {
        return courseService.findCoursePic(courseId);
    }

    @Override
    @DeleteMapping("/coursepic/delete")
    public ResponseResult deleteCoursePic(@RequestParam("courseId") String courseId) {
        return courseService.deleteCoursePic(courseId);
    }

    @Override
    @GetMapping("/courseview/{id}")
    public CourseView courseview(@PathVariable("id") String id) {
        return courseService.getCoruseView(id);
    }

    @Override
    @PostMapping("/preview/{id}")
    public CoursePublishResult preview(@PathVariable("id") String id) {
        return courseService.preview(id);

    }

    @Override
    @PostMapping("/publish/{id}")
    public CoursePublishResult publish(@PathVariable("id") String id) {
        return courseService.publish(id);
    }

    @Override
    @PostMapping("/savemedia")
    public ResponseResult saveMedia(@RequestBody TeachplanMedia teachplanMedia) {
        return courseService.saveMedia(teachplanMedia);
    }

    @Override
    @GetMapping("/coursebase/list/{page}/{size}")
    public QueryResponseResult<CourseInfo> findCourseList(@PathVariable("page") int page,
                                                          @PathVariable("size") int size,
                                                          CourseListRequest courseListRequest) {
        //获取当前用户信息
        CicOauth2Util xcOauth2Util = new CicOauth2Util();
        CicOauth2Util.UserJwt userJwt = xcOauth2Util.getUserJwtFromHeader(request);
        //当前用户所属单位的id
        String company_id = userJwt.getCompanyId();
        QueryResponseResult<CourseInfo> queryResponseResult = courseService.findCourseList(company_id, page, size, courseListRequest);
        return queryResponseResult;
    }

    @Override
    @GetMapping("/get/{courseId}")
    public CourseBase getCourseBaseById(@PathVariable("courseId") String courseId) {
        return courseService.getCourseBaseById(courseId);
    }

    @Override
    @PostMapping("/update/{id}")
    public ResponseResult updateCourseBase(@PathVariable("id") String id, @RequestBody CourseBase courseBase) {
        return courseService.updateCourseBase(id, courseBase);
    }

    @Override
    @GetMapping("/market/get/{courseId}")
    public CourseMarket getCourseMarketById(@PathVariable("courseId") String courseId) {
        return courseService.getCourseMarketById(courseId);
    }

    @Override
    @ApiOperation("获取基础课程")
    @GetMapping("/getbase/{courseIds}")
    public List<CourseBase> getBaseCourseByCourseIds(@PathVariable("courseIds") List<String> courseIds) {
        return courseService.getBaseCourseByCourseIds(courseIds);
    }

    @ApiOperation("获取课程名")
    @GetMapping("/getCourseName/{courseId}")
    public CourseBase getCourseName(@PathVariable("courseId") String courseId) {
        return courseService.getCourseName(courseId);
    }

    @Override
    @PostMapping("/market/update/{id}")
    public ResponseResult updateCourseMarket(@PathVariable("id") String id, @RequestBody CourseMarket courseMarket) {
        return courseService.updateCourseMarket(id, courseMarket);
    }

    @Override
    @GetMapping("/category/list")
    public CategoryNode findList() {
        return courseService.findList();
    }

    @Override
    @GetMapping("/sys/dictionary/get/{type}")
    public SysDictionary getByType(@PathVariable("type") String type) {
        return dictionaryRepository.findByDType(type);
    }

    @GetMapping("/findMongodbRes/{courseId}")
    public MediaUrlResult findMongodbRes(@PathVariable("courseId") String courseId) {
        return courseService.findMongodbRes(courseId);
    }

}
